import router from '@/router'
import axios from 'axios'

// const base_url = 'http://www.esunrising.net:55000'
// const base_url = 'http://192.168.175.254:5000/kshx'
// const base_url = 'http://10.7.22.8/kshx'
const base_url = 'http://192.168.1.181:9000/kshx'

// const base_url = 'http://www.esunrising.net:9001/kshx'
// const base_url = 'http://192.168.0.182:5000/api/createReport'
const request = axios.create({
    baseURL: base_url,
    timeout: 50000000000000
})


request.interceptors.request.use((config) => {
    const token = localStorage.getItem('kshxtoken');
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    // config.headers["Content-Type"] = "application/json"
    return config
}, (error) => {
    return Promise.reject(error)
})

request.interceptors.response.use((response) => {
    if (response.data.code === 401) {
        // 清除token，跳转到登录页面
        localStorage.removeItem('kshxtoken')
        router.navigate('/login')
        window.location.reload()
    }
    return response.data
}, (error) => {
    console.log(error)
    if (error.response && error.response.status === 401) {
        localStorage.removeItem('kshxtoken')
        router.navigate('/login')
        // 清除浏览器的报错消息
        window.location.reload()
    }
    return Promise.reject(error)
})

export { request, base_url }